package br.ufrj.dcc.geti.riskanalytics.simulator;

/**
 * Represents the triangular distribution.
 * This distribuion has:
 * mean: (min + 4*top + max) / 6
 * 
 * @author nando
 */
public class Triangular extends Sampleble {

	private double min;
	private double top;
	private double max;

	public Triangular(double minimumValue, double moreProbable, double maximumValue) {
		min = minimumValue;
		top = moreProbable;
		max = maximumValue;
		
//		System.out.println("triangular:" + min + "-" + top + "-" + max);
	}

	public double getSample() {
		double u0 = r.nextDouble();
		if (u0 < (top - min) / (max - min))
			return min + Math.sqrt(u0 * (top - min) * (max - min));
		return max - Math.sqrt((1 - u0) * (max - min) * (max - top));
	}

}
